package bb.lanxing.activity.routebook;

import static android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.common.config.UmengEventConst;
import bb.lanxing.common.engin.DeveloperOptionsActivity;
import bb.lanxing.util.ui.InputHelper;


public class RouteBookSearchActivity extends BaseActivity {
    private EditText editText;
    private TextView searchLushu;
    private RelativeLayout startSearch;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_lushu_search);
        bindView(getWindow().getDecorView());
        initView();
    }

    private void initView() {
        autoFitStatusBarIconColor();
        setupActionBar(true);
        this.editText.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == IME_ACTION_SEARCH) {
                RouteBookSearchActivity.this.search();
                return false;
            }
            return false;
        });
        this.searchLushu.setOnClickListener(view -> RouteBookSearchActivity.this.search());
        this.startSearch.setOnClickListener(view -> RouteBookSearchActivity.this.startActivity(new Intent(RouteBookSearchActivity.this, RouteBookPositionSearchActivity.class)));
        this.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
            }

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (charSequence == null || !"*#*#2886#*#*".equals(charSequence.toString())) {
                    return;
                }
                RouteBookSearchActivity.this.startActivity(new Intent(RouteBookSearchActivity.this.getApplicationContext(), DeveloperOptionsActivity.class));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        InputHelper.showSoftInput(this.editText);
    }

    public void search() {
        String trim = this.editText.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            App.getContext().showMessage(R.string.lushu_search_toast_input_first);
            return;
        }
        MobclickAgent.onEventValue(this, UmengEventConst.LUSHU_SEARCH, null, 1);
        Intent intent = new Intent(this, RouteBookSearchResultActivity.class);
        intent.putExtra("style", 1);
        intent.putExtra("keyword", trim);
        startActivity(intent);
    }

    private void bindView(View view) {
        this.searchLushu = view.findViewById(R.id.searchLushu);
        this.editText = view.findViewById(R.id.editText);
        this.startSearch = view.findViewById(R.id.startSearch);
    }
}
